home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / snip0493.zip / RFIND1ST.C < prev    next >
C/C++ Source or Header  |  1993-04-05  |  4KB  |  82 lines

  1. /*
  2. **  RFIND1ST.C - Our own non-compiler specific find first/next calls
  3. **
  4. **  Original Copyright 1988-1991 by Bob Stout as part of
  5. **  the MicroFirm Function Library (MFL)
  6. **
  7. **  This subset version is functionally identical to the
  8. **  version originally published by the author in Tech Specialist
  9. **  magazine and is hereby donated to the public domain.
  10. */
  11.  
  12. #include        <stdio.h>
  13. #include        <stdlib.h>
  14. #include        <dos.h>
  15. #include        "dirent.h"
  16.  
  17. /************************************************************************/
  18. /*                                                                      */
  19. /* rfind_1st() - Find first matching file                               */
  20. /*                                                                      */
  21. /* Parameters: 1 - Drive, path and filename of file to be found. May    */
  22. /*                 include wildcards                                    */
  23. /*             2 - Attribute of file to search for. Attributes are      */
  24. /*                 described in the MS-DOS manual. The search strategy  */
  25. /*                 is described under DOS call 0x4Eh.                   */
  26. /*             3 - Disk transfer area buffer. If NULL, one will be      */
  27. /*                 malloc'ed.                                           */
  28. /* Returns: Pointer to a struct DSTRUCT. If error, NULL is returned and */
  29. /*          _doserrno is set to the error #.                            */
  30. /*                                                                      */
  31. /************************************************************************/
  32.  
  33. struct DSTRUCT *rfind_1st(char *name, unsigned attribute, struct DSTRUCT *dta)
  34. {
  35.       struct DSTRUCT *my_dta;
  36.       union REGS regs;
  37.  
  38.       if (NULL == dta)
  39.             my_dta = (struct DSTRUCT *)malloc((int)sizeof(struct DSTRUCT));
  40.       else  my_dta = dta;
  41.  
  42.       bdos(0x1A, (unsigned)my_dta, 0);    /* set DTA to my_dta          */
  43.       regs.x.ax = 0x4E00;                 /* find first                 */
  44.       regs.x.dx = (unsigned)name;
  45.       regs.x.cx = attribute;
  46.       intdos(®s, ®s);
  47.       if (regs.x.cflag)                   /* if error                   */
  48.       {
  49.             _doserrno = regs.x.ax;
  50.             if (NULL == dta && my_dta != NULL)
  51.                   free(my_dta);
  52.             return (struct DSTRUCT *) NULL;
  53.       }
  54.       return my_dta;
  55. }
  56.  
  57. /************************************************************************/
  58. /*                                                                      */
  59. /* rfind_nxt() - Find next matching file                                */
  60. /*                                                                      */
  61. /* Parameters: 1 - Pointer to DSTRUCT structure to use                  */
  62. /*                                                                      */
  63. /* Returns: Pointer to struct DSTRUCT,                                  */
  64. /*          NULL if no more matching files found                        */
  65. /*                                                                      */
  66. /************************************************************************/
  67.  
  68. struct DSTRUCT *rfind_nxt(struct DSTRUCT *dta)
  69. {
  70.       union REGS regs;
  71.  
  72.       bdos(0x1A, (unsigned)dta, 0);       /* set DTA to dta             */
  73.       regs.x.ax = 0x4F00;
  74.       intdos(®s,®s);
  75.       if (regs.x.cflag)                   /* if error                   */
  76.       {
  77.             _doserrno = regs.x.ax;
  78.             return (struct DSTRUCT *) NULL;
  79.       }
  80.       return dta;
  81. }
  82.